package game.Menus;

import game.Config.Config;

import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Vector2f;

/**
 * This class is a menu marker used in the advanced menu.
 * @author Sebastian Isheden and Victor Beer
 *
 */
public class MenuMarker {
	Image image;
	
	Vector2f currentPosition;
	Vector2f lastPositon;
	Vector2f newPosition;
	
	float currentScale;
	float lastScale;
	float newScale;
	
	float time;
	float interval;
	
	float height;
	
	/**
	 * Creates a new menu marker.
	 * @param image The image of the menu marker.
	 * @param height The height of the menu marker.
	 */
	public MenuMarker(Image image, float height) {
		this.image = image;
		this.currentPosition = new Vector2f(0,0);
		this.lastPositon = this.currentPosition.copy();
		this.newPosition = this.currentPosition.copy();
		this.interval = Config.ADVANCED_MENU_INTERVAL;
		this.currentScale = image.getWidth();
		this.newScale = this.currentScale;
		this.lastScale = this.currentScale;
		
		this.height = height;
	}
	
	/**
	 * Changes the menu marker positon and scale.
	 * @param newPosition The new position.
	 * @param newScale The new scale.
	 */
	public void change(Vector2f newPosition, float newScale) {
		this.newPosition = newPosition.copy();
		this.lastPositon = this.currentPosition.copy();
		
		this.newScale = newScale;
		this.lastScale = this.currentScale;
		
		this.time = this.interval - this.time;
	}
	
	/**
	 * Updates the menu marker.
	 * @param delta The time since the last update in millisecounds.
	 */
	public void update(int delta) {
		this.time -= delta/1000f;
		if(this.time < 0) {
			this.time = 0;
			this.currentScale = this.newScale;
			this.currentPosition = this.newPosition.copy();
		} else {
			this.currentScale = this.lastScale + (1 - this.time / this.interval) * (this.newScale - lastScale);
			Vector2f positionDifference = this.newPosition.copy().add(this.lastPositon.copy().scale(-1));
			this.currentPosition = this.lastPositon.copy().add(positionDifference.scale(1 - this.time/this.interval));
		}
	}
	
	/**
	 * Draws the menu item.
	 * @param The position of the menu the marker is stored in.
	 */
	public void draw(Vector2f position) {
		image.draw(position.x + this.currentPosition.x, position.y + this.currentPosition.y, this.currentScale, this.height);
	}
}
